/* Init UrlRegion info */
#include "area.h"
#include <iostream>
#include <string>
#include <curl/curl.h>
#include <sys/types.h>
#include <boost/regex.hpp>
using namespace std;
using namespace boost;
URL init_region(void){

	URL wlanUrl;
	wlanUrl.wlanUrlRegion.gd="wlan.gd.chntel.com";
	wlanUrl.wlanUrlRegion.bj="wlan.bj.chntel.com";
	wlanUrl.wlanUrlRegion.tj="wlan.tj.chntel.com";
	wlanUrl.wlanUrlRegion.hi="wlan.hi.chntel.com";
	wlanUrl.wlanUrlRegion.he="wlan.he.chntel.com";
	wlanUrl.wlanUrlRegion.gx="wlan.gx.chntel.com";
	wlanUrl.wlanUrlRegion.sx="wlan.sx.chntel.com";
	wlanUrl.wlanUrlRegion.nm="wlan.nm.chntel.com";
	wlanUrl.wlanUrlRegion.cq="wlan.cq.chntel.com";
	wlanUrl.wlanUrlRegion.ln="wlan.ln.chntel.com";
	wlanUrl.wlanUrlRegion.gz="wlan.gz.chntel.com";
	wlanUrl.wlanUrlRegion.jl="wlan.jl.chntel.com";
	wlanUrl.wlanUrlRegion.yn="wlan.yn.chntel.com";
	wlanUrl.wlanUrlRegion.hl="wlan.hl.chntel.com";
	wlanUrl.wlanUrlRegion.xz="wlan.xz.chntel.com";
	wlanUrl.wlanUrlRegion.hb="wlan.hb.chntel.com";
	wlanUrl.wlanUrlRegion.sn="wlan.sn.chntel.com";
	wlanUrl.wlanUrlRegion.hn="wlan.hn.chntel.com";
	wlanUrl.wlanUrlRegion.gs="wlan.gs.chntel.com";
	wlanUrl.wlanUrlRegion.ha="wlan.ha.chntel.com";
	wlanUrl.wlanUrlRegion.qh="wlan.qh.chntel.com";
	wlanUrl.wlanUrlRegion.js="wlan.js.chntel.com";
	wlanUrl.wlanUrlRegion.nx="wlan.nx.chntel.com";
	wlanUrl.wlanUrlRegion.sd="wlan.sd.chntel.com";
	wlanUrl.wlanUrlRegion.xj="wlan.xj.chntel.com";
	wlanUrl.wlanUrlRegion.fj="wlan.fj.chntel.com";
	wlanUrl.wlanUrlRegion.ah="wlan.ah.chntel.com";
	wlanUrl.wlanUrlRegion.jx="wlan.jx.chntel.com";
	wlanUrl.wlanUrlRegion.am="wlan.am.chntel.com";
	wlanUrl.wlanUrlRegion.sck="wlan.sck.chntel.com";

	wlanUrl.cwUrlRegion.gd="cw.gd.chntel.com";
	wlanUrl.cwUrlRegion.bj="cw.bj.chntel.com";
	wlanUrl.cwUrlRegion.tj="cw.tj.chntel.com";
	wlanUrl.cwUrlRegion.hi="cw.hi.chntel.com";
	wlanUrl.cwUrlRegion.he="cw.he.chntel.com";
	wlanUrl.cwUrlRegion.gx="cw.gx.chntel.com";
	wlanUrl.cwUrlRegion.sx="cw.sx.chntel.com";
	wlanUrl.cwUrlRegion.nm="cw.nm.chntel.com";
	wlanUrl.cwUrlRegion.cq="cw.cq.chntel.com";
	wlanUrl.cwUrlRegion.ln="cw.ln.chntel.com";
	wlanUrl.cwUrlRegion.gz="cw.gz.chntel.com";
	wlanUrl.cwUrlRegion.jl="cw.jl.chntel.com";
	wlanUrl.cwUrlRegion.yn="cw.yn.chntel.com";
	wlanUrl.cwUrlRegion.hl="cw.hl.chntel.com";
	wlanUrl.cwUrlRegion.xz="cw.xz.chntel.com";
	wlanUrl.cwUrlRegion.hb="cw.hb.chntel.com";
	wlanUrl.cwUrlRegion.sn="cw.sn.chntel.com";
	wlanUrl.cwUrlRegion.hn="cw.hn.chntel.com";
	wlanUrl.cwUrlRegion.gs="cw.gs.chntel.com";
	wlanUrl.cwUrlRegion.ha="cw.ha.chntel.com";
	wlanUrl.cwUrlRegion.qh="cw.qh.chntel.com";
	wlanUrl.cwUrlRegion.js="cw.js.chntel.com";
	wlanUrl.cwUrlRegion.nx="cw.nx.chntel.com";
	wlanUrl.cwUrlRegion.sd="cw.sd.chntel.com";
	wlanUrl.cwUrlRegion.xj="cw.xj.chntel.com";
	wlanUrl.cwUrlRegion.fj="cw.fj.chntel.com";
	wlanUrl.cwUrlRegion.ah="cw.ah.chntel.com";
	wlanUrl.cwUrlRegion.jx="cw.jx.chntel.com";
	wlanUrl.cwUrlRegion.am="cw.am.chntel.com";
	wlanUrl.cwUrlRegion.sck="cw.sck.chntel.com";
	
	return wlanUrl;
}

string get_url(string username, string password, string paramStr, string urlRegion)
{	
	string baseurl="https://wlan.ct10000.com";
	string url, str1, str2, str3, str4, str5, str6, str7;
	str1="/authServlet?PassWord=";
	str2="&PassWord1=&PassWord2=";
	str3="&UserName=";
	str4="&UserName1=&UserName2=";
	str5="&UserType=2&isChCardUser=false&isVerifycode=false&isWCardUser=false&paramStr=";
	str6="&prov=ah&province=";
	str7="&verifycode=";
	url = baseurl + str1 + password + str2 + password + str3 + username + str4 + username + str5 + paramStr + str6 + urlRegion + str7;
	return url;
}

char *getstr;
size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
  size_t str_nmemb = nmemb;
  getstr = (char *)ptr;
  return str_nmemb;
}

string get_paramStr()
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
    
    curl_easy_setopt(curl, CURLOPT_URL, "https://wlan.ct10000.com/");
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
    
    curl_easy_perform(curl);
	
    /* always cleanup */
    curl_easy_cleanup(curl);
  }

  /* search paramStr String */
  regex expression("(?<=paramStr\\=).*?(?=\")");
  cmatch result;
  string param;
  if (getstr != NULL){
  	if (regex_search(getstr, result, expression))
   	 param = result[0];
}
  return param;
}

char *checkstr;
size_t check_str(void *ptr, size_t size, size_t nmemb, void *stream)
{
  size_t str_nmemb = nmemb;
  checkstr = (char *)ptr;
  return str_nmemb;
}

int check_state(void){
  CURL *curl;
  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http://www.baidu.com/");
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, check_str);
    curl_easy_perform(curl);
    /* always cleanup */
    curl_easy_cleanup(curl);
  }
   /* search paramStr String */
  regex expression("baidu");
  cmatch result;
  string param;
  if (checkstr != NULL){
  	if (regex_search(checkstr, result, expression))
		cout << result[0] << endl;
   		 return 1;
	}
	return 0;	
}

